<template>
	<el-main>
		<el-card class="box-card" shadow="never">
			<el-button type="primary" icon="el-icon-plus" @click="addTopCate">添加顶级分类</el-button>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 10px"></div>
			<el-table
				:data="tableData"
				style="width: 100%;"
				row-key="id"
				:tree-props="{children: 'children'}">
				<el-table-column
					type=""
					prop="id"
					label="ID">
				</el-table-column>
				<el-table-column
					prop="name"
					label="分类名">
				</el-table-column>
				<el-table-column
					label="图标">
					<template #default="scope">
						<el-image style="width: 30px; height: 30px" :src="scope.row.icon"></el-image>
					</template>
				</el-table-column>
				<el-table-column
					prop="sort"
					label="排序">
				</el-table-column>
				<el-table-column
					prop="status"
					label="是否显示">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">显示</el-tag>
						<el-tag type="danger" v-else>隐藏</el-tag>
					</template>
				</el-table-column>
				<el-table-column
					prop="create_time"
					label="添加时间">
				</el-table-column>
				<el-table-column
					prop="operation"
					label="操作">
					<template #default="scope">
						<el-button @click="handleAddSub(scope.row)" type="text" size="small"
								   v-if="scope.row.level != 2">子分类
						</el-button>
						<el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
						<el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>

		</el-card>
	</el-main>
</template>

<script>
export default {
	name: "goodsCate",
	data() {
		return {
			tableData: [],
			form: {}
		}
	},
	mounted() {
		this.getList()
	},
	methods: {
		// 添加顶级分类
		addTopCate() {
			this.$router.push({path: '/goodsCate/add', query: {pid: 0, pname: '顶级节点'}})
		},
		// 获取列表
		async getList() {
			let res = await this.$API.goodsCate.list.get()
			this.tableData = res.data
		},
		// 添加子分类
		handleAddSub(item) {
			this.$router.push({path: '/goodsCate/add', query: {pid: item.id, pname: item.name}})
		},
		// 编辑子分类
		handleEdit(item) {
			this.$router.push({path: '/goodsCate/edit', query: {pid: item.id, pname: item.name, id: item.id}})
		},
		handleDel(item) {
			this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				let res = await this.$API.goodsCate.del.get({id: item.id})
				if (res.code == 0) {
					this.$message.success(res.msg)
					this.getList()
				} else {
					this.$message.error(res.msg)
				}
			}).catch(() => {});
		}
	}
}
</script>

<style scoped>
.el-button--small {
	padding: 0 !important;
}
</style>
